const bcrypt = require('bcryptjs');
const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();
    const hashedPassword = await bcrypt.hash('Admin@123456', 10);

    // 先删除已存在的数据（如果有）
    await queryInterface.bulkDelete('users', null, {});
    await queryInterface.bulkDelete('roles', null, {});
    await queryInterface.bulkDelete('tenants', null, {});
    await queryInterface.bulkDelete('plans', null, {});

    // 1. 创建套餐
    const freePlanId = uuidv4();
    const proPlanId = uuidv4();
    const enterprisePlanId = uuidv4();

    await queryInterface.bulkInsert('plans', [
      {
        id: freePlanId,
        name: '免费版',
        code: 'free',
        description: '适合个人用户和小团队',
        price: 0,
        billing_cycle_code: 'monthly',
        price_monthly: 0,
        price_yearly: 0,
        max_users: 5,
        max_storage: 1073741824, // 1GB
        max_projects: 3,
        support_level: 'basic',
        has_api_access: false,
        has_custom_domain: false,
        has_white_label: false,
        has_sso: false,
        has_advanced_analytics: false,
        is_active: true,
        sort_order: 1,
        created_at: now,
        updated_at: now,
      },
      {
        id: proPlanId,
        name: '专业版',
        code: 'professional',
        description: '适合中小型企业',
        price: 99,
        billing_cycle_code: 'monthly',
        price_monthly: 99,
        price_yearly: 999,
        max_users: 50,
        max_storage: 10737418240, // 10GB
        max_projects: 50,
        support_level: 'priority',
        has_api_access: true,
        has_custom_domain: true,
        has_white_label: false,
        has_sso: false,
        has_advanced_analytics: true,
        is_active: true,
        sort_order: 2,
        created_at: now,
        updated_at: now,
      },
      {
        id: enterprisePlanId,
        name: '企业版',
        code: 'enterprise',
        description: '适合大型企业',
        price: 499,
        billing_cycle_code: 'monthly',
        price_monthly: 499,
        price_yearly: 4999,
        max_users: null, // 无限制
        max_storage: null, // 无限制
        max_projects: null, // 无限制
        support_level: 'dedicated',
        has_api_access: true,
        has_custom_domain: true,
        has_white_label: true,
        has_sso: true,
        has_advanced_analytics: true,
        is_active: true,
        sort_order: 3,
        created_at: now,
        updated_at: now,
      },
    ]);

    // 2. 创建租户
    const systemTenantId = uuidv4();

    await queryInterface.bulkInsert('tenants', [
      {
        id: systemTenantId,
        name: '系统管理',
        subdomain: 'system',
        plan_id: enterprisePlanId,
        default_password: 'Pass123456', // 租户默认密码
        status: 'active',
        created_at: now,
        updated_at: now,
      },
    ]);

    // 3. 创建角色
    const superAdminRoleId = uuidv4();

    await queryInterface.bulkInsert('roles', [
      {
        id: superAdminRoleId,
        tenant_id: null, // 系统角色
        name: '超级管理员',
        description: '系统超级管理员',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
    ]);

    // 4. 创建超级管理员用户
    await queryInterface.bulkInsert('users', [
      {
        id: uuidv4(),
        tenant_id: systemTenantId,
        role_id: superAdminRoleId,
        email: 'admin@saas.com',
        password_hash: hashedPassword,
        first_name: 'Super',
        last_name: 'Admin',
        type: 'super_admin',
        status: 'active',
        created_at: now,
        updated_at: now,
      },
    ]);
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('users', null, {});
    await queryInterface.bulkDelete('roles', null, {});
    await queryInterface.bulkDelete('tenants', null, {});
    await queryInterface.bulkDelete('plans', null, {});
  },
};

